//
//  AXFShopPayViewController.m
//  AXFEightGroup
//
//  Created by ya on 12/28/16.
//  Copyright © 2016 ya. All rights reserved.
//

#import "AXFShopPayViewController.h"
#import "AXFShopPayOkeyView.h"




typedef NS_ENUM(NSUInteger, AXFShopPaySection) {
    AXFShopPaySectionDiscount       = 0,
    AXFShopPaySectionMethods        = 1,
    AXFShopPaySectionProducts       = 2,
    AXFShopPaySectionCostDetail     = 3
};







static NSString *const defaultTestID                    = @"defaultTestID";
static NSString *const AXFShopPayFirstCellID            = @"AXFShopPayFirstCellID";
static NSString *const AXFShopPayMethodsCellID          = @"AXFShopPayMethodsCellID";
static NSString *const AXFShopPayProductCellID          = @"AXFShopPayProductCellID";
static NSString *const AXFShopPayAmountPriceCellID      = @"AXFShopPayAmountPriceCellID";
static NSString *const AXFShopPayCostDetailCellID       = @"AXFShopPayCostDetailCellID";



@interface AXFShopPayViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView        *payTableView;
@property (nonatomic, strong) NSArray<AXFDataModel *>   *models;
@property (nonatomic, strong) AXFShopPayOkeyView        *okeyView;
@property (nonatomic, strong) NSArray<AXFDataModel *>   *tmpCostDetilData;

@end

@implementation AXFShopPayViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    [self registerCells];

    [self UILayout];


    // Do any additional setup after loading the view from its nib.
}



#pragma mark - UITableViewDelegate


///


#pragma mark -

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    NSInteger sections = 4;
    return sections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    switch (section)
    {
        case AXFShopPaySectionProducts:
            return self.models.count + 1;

        case AXFShopPaySectionCostDetail:
            return self.tmpCostDetilData.count;

        default:
            return 1;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    CGFloat headerHeight = 40.0;
    return section == AXFShopPaySectionDiscount ? 0.1 : headerHeight;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    switch (section)
    {
        case AXFShopPaySectionMethods:
            return [self sectionLabelText:@"     选择支付方式"    ];

        case AXFShopPaySectionProducts:
            return [self sectionLabelText:@"     精选商品"      ];

        case AXFShopPaySectionCostDetail:
            return [self sectionLabelText:@"     费用明细"      ];

        default:
            break;
    }

    return nil;
}



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    CGFloat footerHeight = 15.0;
    return footerHeight;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *      cellID  = [self configurationCELLIDWithIndexPath:indexPath];

    UITableViewCell *cell   = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil) cell   = [self configurationCELLWithIndexPath:indexPath];

    [self configurationCELLModelWithIndexPath:indexPath cell:cell];

    return cell;
}















#pragma mark -



- (void)registerCells {

    [self.payTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:defaultTestID];

    [self.payTableView registerNib:nib(@"AXFShopPayFirstCell") forCellReuseIdentifier:AXFShopPayFirstCellID];

    [self.payTableView registerNib:nib(@"AXFShopPayMethodsCell") forCellReuseIdentifier:AXFShopPayMethodsCellID];

    [self.payTableView registerNib:nib(@"AXFShopPayProductCell") forCellReuseIdentifier:AXFShopPayProductCellID];

    [self.payTableView registerNib:nib(@"AXFShopPayAmountPriceCell") forCellReuseIdentifier:AXFShopPayAmountPriceCellID];

    [self.payTableView registerNib:nib(@"AXFShopPayCostDetailCell") forCellReuseIdentifier:AXFShopPayCostDetailCellID];

}



- (NSString *)configurationCELLIDWithIndexPath:(NSIndexPath *)indexPath {

    NSString *cellID = nil;

    switch (indexPath.section) {
        case AXFShopPaySectionDiscount:
            cellID = AXFShopPayFirstCellID;
            break;

        case AXFShopPaySectionMethods:
            cellID = AXFShopPayMethodsCellID;
            break;

        case AXFShopPaySectionProducts:
            cellID = (indexPath.row == self.models.count) ? AXFShopPayAmountPriceCellID : AXFShopPayProductCellID;
            break;

        case AXFShopPaySectionCostDetail:
            cellID = AXFShopPayCostDetailCellID;
            break;

        default:
            cellID = defaultTestID;
            break;
    }

    return cellID;
}







- (UITableViewCell *)configurationCELLWithIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = nil;
    switch (indexPath.section) {

        case AXFShopPaySectionDiscount:
            cell = nibView(@"AXFShopPayFirstCell");
            break;

        case AXFShopPaySectionMethods:
            cell = nibView(@"AXFShopPayMethodsCell");
            break;

        case AXFShopPaySectionProducts:
            cell = (indexPath.row == self.models.count) ? nibView(@"AXFShopPayAmountPriceCell") : nibView(@"AXFShopPayProductCell");
            break;

        case AXFShopPaySectionCostDetail:
            cell = nibView(@"AXFShopPayCostDetailCell");
            break;

        default:
            cell = [UITableViewCell new];
            break;
    }
    return cell;
}













- (void)configurationCELLModelWithIndexPath:(NSIndexPath *)indexPath cell:(UITableViewCell *)cell {

    if (indexPath.section == AXFShopPaySectionProducts) {

        NSInteger currentRow    = indexPath.row;
        NSInteger additionalRow = self.models.count; /// 附加的行(总计)

        NSString *totalPriceKey = @"totalPrice";
        NSNumber *totalPrice    = @(_ModelMgr.productTotalPrice); /// model total price


        NSString *modelKey      = @"model";
        AXFDataModel *model     = currentRow != additionalRow ? self.models[currentRow] : nil; /// product model

        currentRow == additionalRow ? ([cell setValue:totalPrice
                                               forKey:totalPriceKey]) : ([cell setValue:model
                                                                                 forKey:modelKey]);
    }

    if (indexPath.section == AXFShopPaySectionCostDetail) {
        [cell setValue:self.tmpCostDetilData[indexPath.row] forKey:@"model"];
    }
}








#pragma mark - 配置

- (void)UILayout {
    self.payTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    self.payTableView.estimatedRowHeight = 100;

    self.okeyView = nibView(@"AXFShopPayOkeyView");
    self.okeyView.totalPrice = [_ModelMgr productTotalPrice] + self.tmpCostDetilData[0].value.floatValue;
    [self.view addSubview:self.okeyView];


    [self.payTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(self.okeyView.mas_top).priority(UILayoutPriorityRequired);
    }];

    [self.okeyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.trailing.leading.equalTo(self.view);
        make.height.equalTo(@(44.0));
    }];
}


- (void)dealloc {
    NSLog(@"[%s dealloc]", __FILE__);
}


- (NSArray<AXFDataModel *> *)tmpCostDetilData {
    if (_tmpCostDetilData == nil) {
        NSDictionary *tmpDict0 = @{@"text"  : @"商品总额",
                                   @"value" : @"8.6"};
        NSDictionary *tmpDict1 = @{@"text"  : @"配送费",
                                   @"value" : @"8.6"};
        NSDictionary *tmpDict2 = @{@"text"  : @"服务费",
                                   @"value" : @"0"};
        NSDictionary *tmpDict3 = @{@"text"  : @"优惠券",
                                   @"value" : @"0"};
        NSMutableArray *tmp = [NSMutableArray new];
        [tmp addObject:[[AXFDataModel alloc] initWithDictionary:tmpDict0]];
        [tmp addObject:[[AXFDataModel alloc] initWithDictionary:tmpDict1]];
        [tmp addObject:[[AXFDataModel alloc] initWithDictionary:tmpDict2]];
        [tmp addObject:[[AXFDataModel alloc] initWithDictionary:tmpDict3]];
        _tmpCostDetilData = tmp.copy;
    }
    return _tmpCostDetilData;
}


- (NSArray<AXFDataModel *> *)models {
    if (_models == nil) _models = [_ModelMgr productList];
    return _models;
}


#pragma mark -

























- (UIView *)sectionLabelText:(NSString *)text {
    UILabel *payMetodsLabel = [UILabel new];
    payMetodsLabel.text = text;
    payMetodsLabel.font = [UIFont boldSystemFontOfSize:17.0];
    [payMetodsLabel sizeToFit];
    payMetodsLabel.backgroundColor = [UIColor whiteColor];
    payMetodsLabel.textColor = [UIColor lightGrayColor];
    return payMetodsLabel;
}

@end
